<template>
  <div>
    <TopFilter ref="searchOrdersPanel" :list="topFilterList" :cols="3" :labelWidth="90" @filterChange="changeHandle" @onCollapse="collapseHandle" />
    <FilterTable
      ref="table"
      selectionType="multiple"
      :columns="columns"
      columnsRef="repairPackage_addPartSelection"
      :datakey="'rows'"
      :height="'250px'"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :isColumnFilter="false"
      :isToperInfo="false"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="tableDbclickFun"
    ></FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button size="small" @click="closeDialog">取消</el-button>
      <el-button size="small" type="primary" class="btnStyle" @click="confirmHandle">确定</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { repairFindPart } from '@/api/repair/repairBusiness/repairPackage/index.js';
export default {
  name: 'repairPackage_addPartSelection',
  mixins: [dictionary],
  props: ['selectData', 'AddProject'],
  data() {
    this.BaseTable = null;
    this.ifEdit = !this.AddProject;
    return {
      topFilterList: this.createFormList(),
      columns: this.createTableColumns(),
      fetchParams: { xhrAbort: false, type: 0 }, // 1厂端 其他都是厂端
      fetchApi: repairFindPart,
      tablelist: []
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    if (this.selectData.length > 0) {
      // this.columns.find(item => item.dataIndex === 'LABOUR_CODE').editItems = this.selectData.map(x => ({ text: x.LABOUR_NAME, value: x.LABOUR_CODE }));
    }
  },
  created() {},
  methods: {
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo',
          rules: [{ min: 2, message: '请输入至少两位配件代码', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'partName'
        }
        // {
        // type: 'SELECT',
        //   label: '车系',
        //   fieldName: 'SERIES_NAME',
        // },
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          sorter: true
        },
        {
          title: '配件名称',
          dataIndex: 'PART_NAME',
          sorter: true
        },
        {
          title: '单位',
          dataIndex: 'UNIT_CODE',
          hidden: true,
          sorter: true
        },
        {
          title: '单位',
          dataIndex: 'UNIT_NAME',
          sorter: true
        },
        {
          title: '数量',
          dataIndex: 'PART_QUANTITY',
          sorter: true,
          editable: true,
          editItems: [],
          editType: 'number',
          precision: 2,
          min: 0,
          filterType: 'input',
          rules: [
            { pattern: /^(\-|\+)?\d+(\.\d+)?$/, message: '只允许输入数字', trigger: 'blur' },
            { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }
          ],
          defaultEditable: true
        }

        // {
        //   title: '是否电池',
        //   dataIndex: 'IS_BATTERY',
        //   sorter: true,
        //   itemList: this.createDictList('1004'),
        //   render: props => {
        //     return <span>{props.row.IS_BATTERY == 10041001 ? '是' : '否'}</span>;
        //   }
        // }
      ];
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({ type: 0 }, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    confirmHandle() {
      if (this.tablelist.length === 0) {
        return this.$notify.error({ title: '提示信息', message: '至少选择一个配件!' });
      }
      for (let i = 0; i < this.tablelist.length; i++) {
        let json = {
          STORAGE_CODE: this.tablelist[i].STORAGE_CODE,
          PART_NO: this.tablelist[i].PART_NO,
          PART_NAME: this.tablelist[i].PART_NAME,
          PART_QUANTITY: this.tablelist[i].PART_QUANTITY,
          MODEL_LABOUR_CODE: this.tablelist[i].MODEL_LABOUR_CODE,
          LABOUR_CODE: this.tablelist[i].LABOUR_CODE,
          SALES_PRICE: this.tablelist[i].SALES_PRICE,
          UNIT_CODE: this.tablelist[i].UNIT_CODE,
          UNIT_NAME: this.tablelist[i].UNIT_NAME,
          REMARK: this.tablelist[i].REMARK,
          SALES_PRICE: this.tablelist[i].NOT_TAX_INSTRUCT_PRICE,
          IS_BATTERY: this.tablelist[i].IS_BATTERY
        };
        if (this.ifEdit == true) {
          // if (this.tablelist[i].LABOUR_CODE.length == 0) {
          // return this.$notify.error({ title: '提示信息', message: '请选择的配件所对应的维修项目' });
          // }
        }
        if (this.tablelist[i].PART_QUANTITY <= 0) {
          return this.$notify.error({ title: '提示信息', message: '请填写所选择的配件数量' });
        }
        let formdata = Object.assign({}, json);
        // 判断配件是否存在
        if (formdata.PART_NO != '' || formdata.PART_NAME != '') {
          console.log('值是=====================================', formdata);
          this.$emit('change1', formdata);
        }
      }
    },
    tableDbclickFun(val) {
      this.tablelist = val;
    },
    reset() {
      this.$refs.formPanel.RESET_FORM();
    },
    closeDialog() {
      this.$emit('close');
    }
  }
};
</script>
<style scoped lang="less">
.btn {
  text-align: center;
}
.bg2 {
  margin-top: 10px;
  margin-bottom: 10px;
}
.bg1 {
  border: 1px solid @borderColor;
  border-radius: @borderRadius;
  box-shadow: @boxShadow;
  padding: @modulePadding;
  padding-left: 0;
  padding-top: 15px;
  padding-bottom: 15px;
}
</style>
